package com.hqyj.shop.realm;


import com.hqyj.shop.pojo.Role;
import com.hqyj.shop.pojo.User;
import com.hqyj.shop.service.userService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;

import java.util.HashSet;
import java.util.Set;

/*
认证域
给shiro框架返回系统中用户的认证信息
给shiro框架返回系统中永户的授权信息
 */

public class MyShiroRealm extends AuthorizingRealm {
    @Autowired
    private userService userService;


    //给shiro框架返回系统中永户的授权信息
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        System.out.println("获取授权信息");
        //获取带授权的用户名
        String username = (String) principalCollection.getPrimaryPrincipal();
        //系统中查询待授权用户的权限信息，角色信息，权限信息
        System.out.println(username);
        User user = userService.getUserByUserName(username);


        //组装一个set<String>类型角色信息
        System.out.println(user);
        Set<String> perms = new HashSet<>();

        Set<String> roles = new HashSet<>();
        for(Role role:user.getRoles()){
            System.out.println(role.getName());
            roles.add(role.getName());

            //5表联查，组装信息
        }

        //创建一个授权信息对象
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //赋予角色相关的授权信息
        info.setRoles(roles);
        info.setStringPermissions(perms);
        return info;
    }
    //给shiro框架返回系统中用户的认证信息
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //使用认证令牌获取当前登录的用户名
        System.out.println("123");
        String username = (String)token.getPrincipal();
        //在我们系统中查询用户的认证信息
        User user = userService.getUserByUserName(username);
        System.out.println(user);

        //如果用户不存在，抛出用户不存在的异常
        if (user == null) {
            throw new UnknownAccountException(username+"用户名不存在");
        }
        //在shiro会话域中共享用户信息，以便业务层使用
        Session session = SecurityUtils.getSubject().getSession();

        session.setAttribute("loginUser",user);
        //调用父类的方法获取当前realm对象的name
        String realmName = getName();
        //用三个参数构造方法来构造,用户名，密码，当前认证域的名称(返回用户名和账号)
//        SimpleAuthenticationInfo info=
//                new SimpleAuthenticationInfo(username,user.getUserPass(),realmName);

        //这里的user的password提供的是暗纹密码，还得提供salt，提供的盐为用户的用户名
        ByteSource salyByts = ByteSource.Util.bytes(user.getUserName());
        //使用4个参数够着
        SimpleAuthenticationInfo info=
               new SimpleAuthenticationInfo(username,user.getUserPass(),salyByts,realmName);
        System.out.println("获取认证信息");
        return info;
    }

}
